package com.clannad.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.clannad.eduservice.entity.EduChapter;
import com.clannad.eduservice.entity.EduVideo;
import com.clannad.eduservice.entity.chapter.ChapterVo;
import com.clannad.eduservice.entity.chapter.VideoVo;
import com.clannad.eduservice.mapper.EduChapterMapper;
import com.clannad.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clannad.eduservice.service.EduVideoService;
import com.clannad.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author clannad
 * @since 2022-08-11
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    @Autowired
    private EduVideoService eduVideoService;
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        LambdaQueryWrapper<EduChapter> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EduChapter::getCourseId,courseId);
        List<EduChapter> chapterList = this.list(lambdaQueryWrapper);

        LambdaQueryWrapper<EduVideo> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(EduVideo::getCourseId,courseId);
        List<EduVideo> videoList = eduVideoService.list(lambdaQueryWrapper1);

        List<ChapterVo> chapterVos=new ArrayList<>();

        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter eduChapter = chapterList.get(i);
            ChapterVo chapterVo=new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            String id = chapterVo.getId();
            List<VideoVo> list=new ArrayList<>();
            for (int m = 0; m < videoList.size(); m++) {
                EduVideo eduVideo = videoList.get(m);
                if(eduVideo.getChapterId().equals(id)){
                    VideoVo videoVo=new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    list.add(videoVo);
                }
            }
            chapterVo.setChildren(list);
            chapterVos.add(chapterVo);
        }
        return chapterVos;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        LambdaQueryWrapper<EduVideo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EduVideo::getChapterId,chapterId);
        int count = eduVideoService.count(lambdaQueryWrapper);
        if(count==0){
            boolean b = this.removeById(chapterId);
            return b;
        }
        else{
            throw new GuliException(20001,"章节信息删除失败，确保章节中没有小姐信息后重试");
        }
    }

    @Override
    public void removeChapterBybCourseID(String courseId) {
        LambdaQueryWrapper<EduChapter> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EduChapter::getCourseId,courseId);
        this.remove(lambdaQueryWrapper);
    }
}
